﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace tDonkeySite.Controllers
{
    public class CoursesCache
    {
        List<eDonkeyClassLibrary.Course> cache = null;
        int size = 0; 


        //DON'T INSERT size==0, This is catastrophic!
        public CoursesCache(int size)
        {
            cache = new List<eDonkeyClassLibrary.Course>(size);
            this.size = size;
        }

        public eDonkeyClassLibrary.Course Find(string Number)
        {
            //TODO: when you convert course.number to string remove the ToString() calling.
            int i = cache.FindIndex(c => c.Number.ToString() == Number);
            if(i != -1)
            {
                eDonkeyClassLibrary.Course course = cache[i];
                cache[i] = cache[0];
                cache[0] = course;
                return course;
            }
            return null;
        }

        //Use it just if the course is not in the cache, I don't want to check if it exist each time I call the function, HAVAL!
        public void Add(eDonkeyClassLibrary.Course course) 
        {
            if (cache.Count == size)
            {
                cache.RemoveAt(cache.Count - 1);
            }
            cache.Insert(0, course);
        }
    }
}